package com.mentor.mate.academy.mealcalculator;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MealCalculator extends Activity implements OnClickListener,
		OnCheckedChangeListener, OnSeekBarChangeListener {
	private static final double DELIVERY = 10; // in euro
	private static final double EURO = 1; //
	private static final double DOLAR = 1.239;//
	private static final double BGN = 1.957; //

	private static final String EURO_SIGN = "€";
	private static final String DOLAR_SIGN = "$";
	private static final String BGN_SIGN = "BGN";

	private static final double DISH_PRICE = 5;// in euro;
	private static final double DESERT_PRICE = 2;// in euro;
	private static final double DRINKK_PRICE = 2;// in euro;
	private Button about, euro, bgn, dolar, dishAdd, dishRemove, desertAdd,
			desertRemove, calculate;

	private TextView dishPrice, desertPrice, drinkPrice, dishQuantity,
			desertQuantity, total;

	private CheckBox homeDelivery;

	private double totalPrice;
	private SeekBar drink;
	private double currentCurrency;
	private String currentCurrencySign;
	private int quantityDish = 0;
	private int quantityDesert = 0;
	private NumberFormat formatter;
	private int dishes;
	private int desert;
	private double drinkLiters;
	private boolean deliveryAtHome = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_meal_calculator);
		formatter = new DecimalFormat("#0.00");

		currentCurrency = EURO; // euro
		currentCurrencySign = EURO_SIGN; // euro
		initLayoutComponents();
		dishPrice.setText(format(DISH_PRICE) + EURO_SIGN);
		desertPrice.setText(format(DESERT_PRICE) + EURO_SIGN);
		drinkPrice.setText(format(DRINKK_PRICE) + EURO_SIGN + "/liter");

		dishQuantity.setText(Integer.toString(quantityDish));
		desertQuantity.setText(Integer.toString(quantityDesert));
	}

	private void initLayoutComponents() {
		// add buttons
		about = (Button) findViewById(R.id.about);
		euro = (Button) findViewById(R.id.euro);
		bgn = (Button) findViewById(R.id.bgn);
		dolar = (Button) findViewById(R.id.dolar);
		dishAdd = (Button) findViewById(R.id.addDish);
		dishRemove = (Button) findViewById(R.id.removeDish);
		desertAdd = (Button) findViewById(R.id.addDesert);
		desertRemove = (Button) findViewById(R.id.removeDesert);
		calculate = (Button) findViewById(R.id.calculate);
		// add TextView
		dishPrice = (TextView) findViewById(R.id.dishPrice);
		dishQuantity = (TextView) findViewById(R.id.dishdQuantity);

		desertPrice = (TextView) findViewById(R.id.desertPrice);
		desertQuantity = (TextView) findViewById(R.id.desertQuantity);

		drinkPrice = (TextView) findViewById(R.id.drinkPrice);

		total = (TextView) findViewById(R.id.total);

		homeDelivery = (CheckBox) findViewById(R.id.homeDelivery);

		// seek bar
		drink = (SeekBar) findViewById(R.id.seekBar1);
		drink.setMax(6); // 0 , 0.5 , 1 , 1.5 , 2, 2.5 3.0 liters
		// add listenr to seekbar
		drink.setOnSeekBarChangeListener(this);

		// add listeners to buttons
		about.setOnClickListener(this);
		euro.setOnClickListener(this);
		bgn.setOnClickListener(this);
		dolar.setOnClickListener(this);
		dishAdd.setOnClickListener(this);
		dishRemove.setOnClickListener(this);
		desertAdd.setOnClickListener(this);
		desertRemove.setOnClickListener(this);
		calculate.setOnClickListener(this);
		homeDelivery.setOnCheckedChangeListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.about:
			Intent aboutIntent = new Intent(getApplicationContext(),
					About.class);
			startActivity(aboutIntent);
			break;
		case R.id.euro:
			changeCurrenty(EURO, EURO_SIGN);
			break;
		case R.id.dolar:
			changeCurrenty(DOLAR, DOLAR_SIGN);
			break;
		case R.id.bgn:
			changeCurrenty(BGN, BGN_SIGN);
			break;
		case R.id.addDish:
			updateDish("add");
			break;
		case R.id.removeDish:
			updateDish("remove");
			break;
		case R.id.addDesert:
			updateDesert("add");
			break;
		case R.id.removeDesert:
			updateDesert("remove");
			break;
		case R.id.calculate:
			calculate();
			break;
		default:
			break;
		}

	}

	private void calculate() {
		double dishesTotalPrice = dishes * DISH_PRICE * currentCurrency;
		double drinkTotalPrice = drinkLiters * DRINKK_PRICE * currentCurrency;
		double desertsTotalPrice = desert * DESERT_PRICE * currentCurrency;
		if (deliveryAtHome) {
			double delivery = 10 * currentCurrency;
			totalPrice = dishesTotalPrice + drinkTotalPrice + desertsTotalPrice
					+ delivery;

		} else {
			totalPrice = dishesTotalPrice + drinkTotalPrice + desertsTotalPrice;
		}
		updatePrice();
	}

	private void updateDish(String update) {
		if (update.equals("add")) {
			if (dishes < 10) {
				dishes++;
			}
		}
		if (update.equals("remove")) {
			if (dishes > 0) {
				dishes--;
			}
		}
		dishQuantity.setText(Integer.toString(dishes));
	}

	private void updateDesert(String update) {
		if (update.equals("add")) {
			if (desert < 10) {
				desert++;
			}
		}
		if (update.equals("remove")) {
			if (desert > 0) {
				desert--;
			}
		}
		desertQuantity.setText(Integer.toString(desert));
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.homeDelivery:
			if (isChecked) {
				totalPrice += DELIVERY * currentCurrency;
			} else {
				totalPrice -= DELIVERY * currentCurrency;
			}
			deliveryAtHome = isChecked;
			updatePrice();
			break;
		default:
			break;
		}
	}

	private void changeCurrenty(double curency, String currencySign) {
		// convert current currancy to euro
		if (currentCurrency == DOLAR) {
			totalPrice /= DOLAR;
		}
		if (currentCurrency == BGN) {
			totalPrice /= BGN;
		}

		totalPrice *= curency;
		currentCurrency = curency;
		currentCurrencySign = currencySign;
		updatePrice();
		updatePrices();
	}

	private void updatePrices() {
		dishPrice.setText(format(DISH_PRICE * currentCurrency) + " "
				+ currentCurrencySign);
		desertPrice.setText(format(DESERT_PRICE * currentCurrency) + " "
				+ currentCurrencySign);
		drinkPrice.setText(format(DRINKK_PRICE * currentCurrency) + " "
				+ currentCurrencySign);
	}

	private void updatePrice() {
		total.setText(format(totalPrice) + " " + currentCurrencySign);
	}

	private String format(double doubleToFormat) {
		return formatter.format(doubleToFormat);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		drinkLiters = progress * 0.5;
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {

	}
}
